package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrOctetString;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * ProxyTransCommandRequest类
 * 实现代理传输命令请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ProxyTransCommandRequest implements AxdrType {

    private byte[] dataCode = null;
    private PIID piid = null;
    private OAD oad = null;
    private COMDCB comdcb = null;
    private Unsigned16 recvIdleSec = null;
    private Unsigned16 recvIdleByte = null;
    private AxdrOctetString transCmd = null;

    public ProxyTransCommandRequest() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid, OAD oad, COMDCB comdcb, Unsigned16 recvIdleSec, Unsigned16 recvIdleByte,
            AxdrOctetString transCmd) {
        this.piid = piid;
        this.oad = oad;
        this.comdcb = comdcb;
        this.recvIdleSec = recvIdleSec;
        this.recvIdleByte = recvIdleByte;
        this.transCmd = transCmd;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.comdcb = new COMDCB();
        codeLength += this.comdcb.decode(input);

        this.recvIdleSec = new Unsigned16();
        this.recvIdleSec.setConst();
        codeLength += this.recvIdleSec.decode(input);

        this.recvIdleByte = new Unsigned16();
        this.recvIdleByte.setConst();
        codeLength += this.recvIdleByte.decode(input);

        this.transCmd = new AxdrOctetString();
        codeLength += this.transCmd.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.transCmd != null && this.recvIdleByte != null && this.recvIdleSec != null
                    && this.comdcb != null && this.oad != null && this.piid != null) {
                codeLength = 0;
                codeLength += this.transCmd.encode(output);
                codeLength += this.recvIdleByte.encode(output);
                codeLength += this.recvIdleSec.encode(output);
                codeLength += this.comdcb.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new RuntimeException("transCmd || recvIdleByte || recvIdleSec || comdcb || oad || piid is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding ProxyTransCommandRequest", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.transCmd != null && this.recvIdleByte != null && this.recvIdleSec != null
                && this.comdcb != null && this.oad != null && this.piid != null) {
            return "sequence: {" + "piid: " + this.piid + ", oad: " + this.oad + ", comdcb: " + this.comdcb
                    + ", recvidlesec: " + this.recvIdleSec + ", recvidlebyte: " + this.recvIdleByte + ", transcmd: "
                    + this.transCmd + "}";
        } else {
            return "transCmd || recvIdleByte || recvIdleSec || comdcb || oad || piid is null";
        }
    }

    public PIID getPiid() {
        return piid;
    }

    public OAD getOad() {
        return oad;
    }

    public COMDCB getComdcb() {
        return comdcb;
    }

    public Unsigned16 getRecvIdleSec() {
        return recvIdleSec;
    }

    public Unsigned16 getRecvIdleByte() {
        return recvIdleByte;
    }

    public AxdrOctetString getTransCmd() {
        return transCmd;
    }
}